package com.gilly;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class MemberDAO {
	DataSource dataFactory;
	public MemberDAO(){
		try{
			Context ctx = new InitialContext();
			dataFactory = (DataSource)ctx.lookup("java:comp/env/jdbc/Oracle11g");
		}catch(NamingException e){
			e.printStackTrace();
		}
	}
	
	public void insert(ArrayList<MemberDTO> list){
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try{
			conn = dataFactory.getConnection(); 
			String sql = "insert into userinfo(no, id, password, name, address, email, phone1, phone2, phone3)"+ 
					" values(userinfoseq.nextval,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, list.get(0).getId());
			pstmt.setString(2, list.get(0).getPassword());
			pstmt.setString(3, list.get(0).getName());
			pstmt.setString(4, list.get(0).getAddress());
			pstmt.setString(5, list.get(0).getEmail());
			pstmt.setString(6, list.get(0).getPhone1());
			pstmt.setString(7, list.get(0).getPhone2());
			pstmt.setString(8, list.get(0).getPhone3());
			int e = pstmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(pstmt!=null) pstmt.close();
				if(conn !=null) pstmt.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
	}
}
